package com.xintong.visualinspection.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.Organ;
import com.xintong.visualinspection.service.DepartmentService;

/**
 * 文件名：TestController
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
/**
 * @author wenhongquan
 *
 */
@RestController
@RequestMapping("/dept")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;
    
    /**
     * 添加部门
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value = "/addDept",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
    public String addDept(@RequestBody Organ organ) throws Exception{
        
    	departmentService.insert(organ);
    	return returnResult(0, "添加成功", null);
    }
    
    /**
     * 修改部门
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/updateDept/{organid}",method=RequestMethod.PUT,produces="application/json;charset=UTF-8")
    public String updateDept(@RequestBody Organ organ,@PathVariable int organid){
        try{
        	organ.setId(organid);
        	departmentService.update(organ);
        	return super.returnResult(0, "修改成功", null);
        }catch(Exception e){
        	throw new BusinessException(20003);
        }
    }
    
    /**
     * 删除部门（软删除）
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/deleteDept/{organid}",method=RequestMethod.DELETE,produces="application/json;charset=UTF-8")
    public String deleteDept(@PathVariable Integer organid){
        try{
        	departmentService.delete((organid));
        	return returnResult(0, "删除成功", null);
        }catch(Exception e){
        	throw new BusinessException(20002);
        }
    }
    
    /**
     * 获取所用部门
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/get/all",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getAllDept(){
        try{
        	List<Organ> organs= departmentService.getOrgans(new Organ());
        	return returnResult(0, "获取成功", organs);
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
    
    @RequestMapping(value = "/getDeptByParent/{pid}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getDeptByParent(@PathVariable Integer pid){
        try{
   
        	Organ organ = new Organ();
        	organ.setParentid(pid);
        	List<Organ> organs= departmentService.getOrgans(organ);
        	return returnResult(0, "获取成功",(organs));
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
    
    @RequestMapping(value = "/getDeptById/{organid}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
    public String getDeptById(@PathVariable Integer organid){
        try{
        	Organ organ = new Organ();
        	organ.setId(organid);
        	
        	List<Organ> organs = departmentService.getOrgans(organ);
        	if(organs==null||organs.size()<1 ) throw  new BusinessException(20001);
        	return returnResult(0, "获取成功", organs.get(0));
        }catch(Exception e){
        	throw new BusinessException(20001);
        }
    }
   
    
}
